﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace EarthImitate
{
    public class Orbit
    {
        #region Members

        private int phanHoach;     
        Point2D gocToaDo;       
        private double heSoX;
        private int banKinh;      
        private double heSoY;       
        private Wireframe wireFrameDisplay = new Wireframe();
        private Wireframe wireFrame = new Wireframe();
        private int heSoa = 0;      
        private int heSob = 0;
        private Color colorLine;
        #endregion
        #region Constructor
        public Orbit(Point2D p0QD)
        {
            this.gocToaDo = p0QD;
            this.phanHoach = 200;
            this.banKinh = 150;
            this.heSoY = 1.75;
            this.heSoX = 3;
            this.heSoa = 0;
            this.heSob = 0;
            this.colorLine = Color.White;
            InitWireFrame();
        }
        public Orbit(int banKinh, int phanHoach, float goc, double heSoX,
            double heSoY, int heSoa, int heSob, Point2D gocToaDo, Color colorLine)
        {
            this.gocToaDo = gocToaDo;
            this.phanHoach = phanHoach;
            this.banKinh = banKinh;
            this.heSoY = heSoY;
            this.heSoX = heSoX;
            this.heSoa = heSoa;
            this.heSob = heSob;
            this.colorLine = colorLine;
            InitWireFrame();
        }

        #endregion

        #region Properties

        public Point2D GocToaDo
        {
            get { return gocToaDo; }
            set { gocToaDo = value; }
        }
        public int PhanHoach
        {
            get { return phanHoach; }
            set { phanHoach = value; }
        }
        public double HeSoY
        {
            get { return heSoY; }
            set { heSoY = value; }
        }
        public double HeSoX
        {
            get { return heSoX; }
            set { heSoX = value; }
        }
        public int RQuyDao
        {
            get { return banKinh; }
            set { banKinh = value; }
        }
        public int HeSob
        {
            get { return heSob; }
            set { heSob = value; }
        }
        public int HeSoa
        {
            get { return heSoa; }
            set { heSoa = value; }
        }

        public Wireframe WireFrame
        {
            get { return wireFrame; }
            set { wireFrame = value; }
        }
        public Color ColorLine
        {
            get { return colorLine; }
            set { colorLine = value; }
        }
        #endregion

        #region Methods

        private void InitWireFrame()
        {

            wireFrame.NumEdges = phanHoach;
            wireFrame.NumVertex = phanHoach + 1;
            wireFrame.Vertexes = new Point3D[wireFrame.NumVertex];
            wireFrame.Edges = new int[wireFrame.NumEdges, 2];

            wireFrameDisplay.NumEdges = phanHoach;
            wireFrameDisplay.NumVertex = phanHoach + 1;
            wireFrameDisplay.Vertexes = new Point3D[wireFrame.NumVertex];
            wireFrameDisplay.Edges = new int[wireFrame.NumEdges, 2];

          
            double a = heSoa;
            double b = heSob;
            double da = (2 * Math.PI) / phanHoach;


            for (int i = 0; i < phanHoach; i++)
            {
                // 1.
                wireFrame.Vertexes[i].z = 0;
                wireFrame.Vertexes[i].y = heSoY * banKinh * Math.Cos(a);
                wireFrame.Vertexes[i].x = heSoX * banKinh * Math.Sin(b);

                wireFrameDisplay.Vertexes[i].z = 0;
                wireFrameDisplay.Vertexes[i].y = heSoY * banKinh * Math.Cos(-a);
                wireFrameDisplay.Vertexes[i].x = heSoX * banKinh * Math.Sin(-b);

                a += da;
                b += da;

            }

            for (int i = 0; i < phanHoach - 1; i++)
            {
                wireFrame.Edges[i, 0] = i;
                wireFrame.Edges[i, 1] = i + 1;

                wireFrameDisplay.Edges[i, 0] = i;
                wireFrameDisplay.Edges[i, 1] = i + 1;

            }

            wireFrame.Edges[phanHoach - 1, 0] = 0;
            wireFrame.Edges[phanHoach - 1, 1] = phanHoach - 1;

            wireFrameDisplay.Edges[phanHoach - 1, 0] = 0;
            wireFrameDisplay.Edges[phanHoach - 1, 1] = phanHoach - 1;
        }
        public void Draw(Graphics graphics)
        {
            for (int i = 0; i < wireFrameDisplay.NumEdges; i++)
            {
                Point2D p1 = Drawing.chieuSSoxy(wireFrameDisplay.Vertexes[wireFrameDisplay.Edges[i, 0]]);
                Point2D p2 = Drawing.chieuSSoxy(wireFrameDisplay.Vertexes[wireFrameDisplay.Edges[i, 1]]);
                Drawing.MyLine(gocToaDo, colorLine, p1, p2, graphics);
            }
        }
        #endregion

    }
}
